package io.eyer.rpc.proxy;

import io.eyer.nio.core.event.Acceptor;
import io.eyer.rpc.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2015/8/24.
 */
public class ServerProxy implements Closeable {
    private static Logger logger = LoggerFactory.getLogger(ServerProxy.class);
    private final Server server;
    public static ConcurrentHashMap<Class<?>, Object> services = new ConcurrentHashMap<>();
    private Acceptor acceptor;
    public ServerProxy(String host, int port,Acceptor acceptor) throws IOException {
        server = new Server(host, port, acceptor);
    }


    public void exportService(Class<?> interfaceClass, Object interfaceImpl) {
        services.put(interfaceClass, interfaceImpl);
    }

    public Object findService(Class<?> interfaceClass) {
        return services.get(interfaceClass);
    }

    public void start() {
        this.server.start();
        logger.info("server proxy  start success {}{}",this.server,this);
    }

    @Override
    public void close(){
        this.server.close();
        logger.info("server proxy  stop success {}{}",this.server,this);
    }
}
